<?php
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	if(!empty($_RQ['code'])){
		$params['code']=trim($_RQ['code']);
		$return['params']['code']=trim($_RQ['code']);
	}
    $fields=array();
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_security_goods',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['type']=array('1'=>'直接添加','2'=>'其他插件调用接口添加');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：如果已经添加了防伪商品，添加防伪批次，请勿修改：参数设置--防伪码生成设置。否则可能导致，已生成的批次不可用！只能删除商品批次重来！','list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'code','title'=>'商品编号','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'type'),
		),
		'action'=>array(
			array('type'=>'href','action'=>'batch','title'=>'批次管理'),
		)
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'code','width'=>15,'title'=>'商品编号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_security_goods',array('id'=>$id));
	}else{
		$item=array('isweishop'=>0);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'模板设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品管理'),
			array('op'=>'edit','title'=>'添加商品')
		),
		'edit'=>array(
			array('name'=>'isweishop','title'=>'商城商品？','type'=>'switch'),
			array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/','show'=>array('isweishop'=>1)),
			array('name'=>'title','title'=>'名称','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'image','title'=>'图片','type'=>'image','show'=>array('isweishop'=>0)),
			array('name'=>'price','title'=>'价格','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'content','title'=>'描述','type'=>'string','show'=>array('isweishop'=>0)),
			array('name'=>'url','title'=>'商品链接','type'=>'string','show'=>array('isweishop'=>0)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['isweishop']){
		$data=pdo_get('jy_weishop_goods',array('id'=>$postdata['goodsid']),array('title','image','price','subtitle'));	
		$data['goodsid']=$postdata['goodsid'];
		$data['content']=$data['subtitle'];
		$data['isweishop']=1;
		unset($data['subtitle']);
	}else{
		$data=array_fields(array('title','price','image','content','url','goodsid','isweishop'),$postdata);	
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_security_goods',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['type']=1;
		$this->model('goods')->add($data);
		exi('添加成功！','','list');
	}
}

if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_security_goods',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_security_goods',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

?>